home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pp / pp-6.0 / Src / submit / submit_srvr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-18  |  3.5 KB  |  163 lines

  1. /* submit_srvr.c: submit running as a server */
  2.  
  3. # ifndef lint
  4. static char Rcsid[] = "@(#)$Header: /xtel/pp/pp-beta/Src/submit/RCS/submit_srvr.c,v 6.0 1991/12/18 20:28:02 jpo Rel $";
  5. # endif
  6.  
  7. /*
  8.  * $Header: /xtel/pp/pp-beta/Src/submit/RCS/submit_srvr.c,v 6.0 1991/12/18 20:28:02 jpo Rel $
  9.  *
  10.  * $Log: submit_srvr.c,v $
  11.  * Revision 6.0  1991/12/18  20:28:02  jpo
  12.  * Release 6.0
  13.  *
  14.  */
  15.  
  16. #include "util.h"
  17. #include <isode/tpkt.h>
  18. #include <fcntl.h>
  19. #include <signal.h>
  20. #include <isode/internet.h>
  21.  
  22. extern char *submit_addr;
  23. extern int  errno;
  24.  
  25. #ifdef BSD42
  26. #include <sys/wait.h>
  27.  
  28. /* ARGSUSED */
  29. static SFD    reaper (sig)
  30. int sig;
  31. {
  32.     union wait status;
  33.     
  34.     while (wait3 (&status, WNOHANG, (struct rusage *)0) > 0)
  35.         continue;
  36. }
  37. #endif
  38.  
  39. int server_start (host, statusp)
  40. char *host;
  41. int *statusp;
  42. {
  43.     struct servent *sp;
  44.     u_short port;
  45.     int    sd;
  46.     struct sockaddr_in s_in;
  47.     int newsd, qfd;
  48.     int argc;
  49.     char *argv[20];
  50.     char *subp;
  51.  
  52.     argc = sstr2arg (submit_addr, 50, argv, ",");
  53.     if (argc <= 0 || (subp = index(argv[0], ':')) == NULL) {
  54.         PP_OPER (NULLCP, ("Bad submit port specification"));
  55.         exit (1);
  56.     }
  57.     *subp ++ = '\0';
  58.         
  59.     if (isdigit (*subp))
  60.         port = htons ((u_short)atoi(subp));
  61.         else if ((sp = getservbyname (subp, "tcp")) != NULL)
  62.         port = sp -> s_port;
  63.     else {
  64.         PP_OPER (NULLCP, ("Can't locate port %s", subp));
  65.         exit (1);
  66.     }
  67.     if (tryfork ()) exit(0);
  68.     if ((sd = open ("/dev/null", O_RDWR)) != NOTOK) {
  69.         if (sd != 0)
  70.             (void) dup2 (sd, 0), (void) close (sd);
  71.         (void) dup2 (0, 1);
  72.         (void) dup2 (0, 2);
  73.     }
  74. #ifdef SETSID
  75.     (void) setsid ();
  76. #endif
  77. #ifdef  TIOCNOTTY
  78. {    int i;
  79.         if ((i = open ("/dev/tty", O_RDWR)) != NOTOK) {
  80.                 (void) ioctl (i, TIOCNOTTY, NULLCP);
  81.                 (void) close (i);
  82.         }
  83. }
  84. #else
  85. #ifdef  SYS5
  86.         (void) setpgrp ();
  87.         (void) signal (SIGINT, SIG_IGN);
  88.         (void) signal (SIGQUIT, SIG_IGN);
  89. #endif
  90. #endif
  91.         isodexport (NULLCP);    /* re-initialize logfiles */
  92.  
  93.     if ((sd = socket (AF_INET, SOCK_STREAM, 0)) == NOTOK) {
  94.         PP_OPER ("socket", ("Can't create "));
  95.         exit (1);
  96.     }
  97.  
  98.     bzero ((char *)&s_in, sizeof s_in);
  99.     s_in.sin_family = AF_INET;
  100.     s_in.sin_port = port;
  101.  
  102.     if (bind (sd, (struct sockaddr *)&s_in, sizeof s_in) == NOTOK ||
  103.         listen(sd, 5) == NOTOK) {
  104.         PP_OPER ("bind/listen", ("failed to "));
  105.         exit (1);
  106.     }
  107.  
  108. #ifdef BSD42
  109.     (void) signal (SIGCHLD, reaper);
  110. #else
  111.     (void) signal (SIGCLD, SIG_IGN);
  112. #endif
  113.  
  114.     PP_NOTICE (("Submit started"));
  115.     for (;;) {
  116.         struct tsapblk *tb;
  117.         qfd = qmgr_start (host, statusp, 0);
  118.  
  119.         PP_TRACE (("qmgr on ad %d, status = %d", qfd, *statusp));
  120.         while ((newsd = accept (sd, (struct sockaddr *)0, NULLIP)) == NOTOK) {
  121.             if (errno != EINTR) {
  122.                 PP_SLOG (LLOG_EXCEPTIONS, "accept",
  123.                      ("problem with"));
  124.                 sleep (1);
  125.             }
  126.         }
  127.         PP_TRACE (("new association on %d", newsd));
  128.         switch (tryfork()) {
  129.             case NOTOK:
  130.             (void) close (newsd);
  131.             break;
  132.  
  133.             case 0: /* child */
  134.             isodexport (NULLCP);
  135.             (void) dup2 (newsd, 0);
  136.             (void) dup2 (newsd, 1);
  137.             (void) close (newsd);
  138.             (void) close (sd);
  139.             ll_close (pp_log_stat);
  140.             ll_close (pp_log_oper);
  141.             ll_close (pp_log_norm);
  142.             return qfd;
  143.  
  144.             default:
  145.             if ((tb = findtblk (qfd)) != (struct tsapblk *) NULL) {
  146.                 if ((*tb -> tb_closefnx) (tb -> tb_fd) == NOTOK)
  147.                     PP_LOG (LLOG_EXCEPTIONS,
  148.                         ("close failed on %d",
  149.                          tb -> tb_fd));
  150.  
  151.                 PP_TRACE (("Closed qmgr %d", tb -> tb_fd));
  152.                 tb -> tb_fd = NOTOK;
  153.                 freetblk (tb);
  154.             }
  155.             if (close (newsd) == NOTOK)
  156.                 PP_LOG (LLOG_EXCEPTIONS, ("close failed on %d",
  157.                               newsd));
  158.             PP_TRACE (("Closed newsd %d", newsd));
  159.             break;
  160.         }
  161.     }
  162. }
  163.